package binary;

/**
 * 1)编译器在编译时会将10进制编译成2进制，然后按照二进制来运算
 * 2)int类型是32位2进制数，显示2进制自动省略高位0
 * 3)Integer.toBinaryString()可以将底层的2进制数返回
 * System.out.println()将2进制转换为10进制输出
 */
public class BinaryDemo {
    public static void main(String[] args) {
        int n = 10;
        System.out.println(Integer.toBinaryString(n));//以2进制输出
        System.out.println(n);//以10进制输出

        n++;
        System.out.println(Integer.toBinaryString(n));
        System.out.println(n);

        int x = 0x2fd47b;
        int y = 0b0010_1111_1101_0100_0111_1011;
        System.out.println(Integer.toBinaryString(x));//按2进制输出
        System.out.println(Integer.toBinaryString(y));
        System.out.println(x);//按10进制输出
        System.out.println(y);
        System.out.println(Integer.toHexString(x));//按16进制输出
        System.out.println(Integer.toHexString(y));

        //规则：逢8进1
        int a = 067;
        System.out.println(a);//55(6个8加7个1)

        //编写4位二进制数
        for (int i = 0; i < 16; i++) {
            System.out.println(Integer.toBinaryString(i));
        }

        //负值的演示
        int b=-544,c=-455;
        System.out.println(Integer.toBinaryString(b));
        System.out.println(Integer.toBinaryString(c));
    }
}
